package com.gagakuai.domain.jude;

import com.gagakuai.domain.facts.Facts;
import com.gagakuai.domain.jude.expression.AbstractRuleJudeExecutor;
import com.gagakuai.domain.jude.expression.AviatorExpressExecutor;
import com.gagakuai.domain.rules.StaticRule;
import com.gagakuai.infrastructure.constant.Constants;
import org.springframework.stereotype.Component;

/**
 * @description: 静态规则表达式执行器
 * @author: houhong
 * @create: 2024-09-07 14:10
 **/
@Component
public class StaticRuleJudeExecutor extends AbstractRuleJudeExecutor<StaticRule> {

    final AviatorExpressExecutor expressExecutor;

    public StaticRuleJudeExecutor(AviatorExpressExecutor expressExecutor) {
        this.expressExecutor = expressExecutor;
    }


    @Override
    public Integer getType() {
        return Constants.STATIC_RULE_TYPE;
    }

    @Override
    public boolean execute(StaticRule rule, Facts facts) {

        // 数据获取
        Facts dataRead = indexDataReadAndCalculate(rule,facts);
        facts.add(dataRead);
        return (Boolean) expressExecutor.execute(rule.getExpression(), facts);
    }
}
